package com.niiwoo.civet.trade.service.local.newRefund.notify.mq;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.ProjectBorrowerSummary;
import com.niiwoo.civet.trade.dao.mapper.ProjectBorrowerSummaryMapperExt;
import com.niiwoo.civet.trade.dto.request.blacklist.BlackListStorageMQDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import com.niiwoo.civet.trade.service.local.refund.CommonLocalService;
import com.niiwoo.civet.trade.utils.RepaymentUtil;
import com.niiwoo.tripod.base.component.PrivacyMasks;

/**
 * 用户中心黑灰名单MQ通知
 * 
 * 触发条件：项目逾期
 *
 * @author: kevin.jia
 * @version 1.0.0
 */
@Component
public class UserBlackListNotify extends MQRefundNotify {

	@Autowired
	private ProjectBorrowerSummaryMapperExt projectBorrowerSummaryMapper;
	
	@Autowired
	private PrivacyMasks privacyMasks;
	
	@Autowired
	private CommonLocalService commonLocalService;
	
	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		Date currentDate = commonLocalService.getCurrentDate();
		
		Project project = refundNotifyDTO.getProject();
		ProjectBorrowerSummary summary = projectBorrowerSummaryMapper.selectByPrimaryKey(refundNotifyDTO.getProjectId());
		
		// 逾期小于30天的不作处理
		if (RepaymentUtil.getOverdueDay(summary.getRefundDate(), currentDate) < 30) {
			return null;
		}
		
		// 灰名单：逾期天数大于等于30天小于90天
		// 黑名单：逾期天数大于等 于90天
		BlackListStorageMQDTO blackListStorageMQDTO = new BlackListStorageMQDTO();
		if (RepaymentUtil.getOverdueDay(summary.getRefundDate(), currentDate) >= 90) {
			blackListStorageMQDTO.setListType((byte) 1);// 黑名单
		} else {
			blackListStorageMQDTO.setListType((byte) 2);// 灰名单
		}
		blackListStorageMQDTO.setUserSource((byte) 3);// 贷后还款
		blackListStorageMQDTO.setIdCard(privacyMasks.decryptPrivacy(project.getBorrowIdCard()));
		blackListStorageMQDTO.setMobileNo(privacyMasks.decryptPrivacy(project.getBorrowMobile()));

		return blackListStorageMQDTO;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.USER_BLACK_LIST;
	}
}